package com.example.home.active_function;

import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.Timer;
import java.util.TimerTask;

public class ActivityFunction extends AppCompatActivity {

    private ViewPager vp_banner;
    private TabLayout rcv_classify;
    private RecyclerView rcv_list;

    private SlideshowAdapter adapter;
    private int currentPage = 0;
    private Timer timer;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function);
        initView();
        //轮播图
        getSlideshow();
        startAutoScroll();

        //分类
        getclassify();
        getNewDate(1);
    }

    private void getclassify() {
        OKHttpTool.doNetRequest("prod-api/api/activity/category/list",
                OKHttpTool.GET,
                ClassifyBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        ClassifyBean classifyBean = (ClassifyBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for (ClassifyBean.DataBean dataBean : classifyBean.getData()){
                                    TabLayout.Tab tab = rcv_classify.newTab();
                                    tab.setText(dataBean.getName());
                                    rcv_classify.addTab(tab);
                                }
                                Getnewslist(classifyBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void Getnewslist(ClassifyBean classifyBean) {
        rcv_classify.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String tabtext = tab.getText().toString();
                if (classifyBean.getData().get(position).getName().equals(tabtext)){
                    getNewDate(classifyBean.getData().get(position).getId());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getNewDate(int id) {
        OKHttpTool.doNetRequest("prod-api/api/activity/activity/list?categoryId="+id,
                OKHttpTool.GET,
                Acti_NewsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        Acti_NewsBean acti_newsBean = (Acti_NewsBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(acti_newsBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindList(Acti_NewsBean acti_newsBean) {
        RecyclerView recyclerView =findViewById(R.id.rcv_list);
        NewsListAdapter newsListAdapter = new NewsListAdapter(ActivityFunction.this,acti_newsBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ActivityFunction.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(newsListAdapter);
    }

    private void getSlideshow() {
        OKHttpTool.doNetRequest("prod-api/api/activity/rotation/list",
                OKHttpTool.GET,
                SlideshowBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        SlideshowBean slideshowBean = (SlideshowBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindSlideshow(slideshowBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindSlideshow(SlideshowBean slideshowBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        adapter = new SlideshowAdapter(this, slideshowBean.getRows());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == adapter.getCount()){
                    currentPage = 0;
                }
                vp_banner.setCurrentItem(currentPage++,true);;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        },DELAY_MS,PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null){
            timer.cancel();
        }
    }

    private void initView() {
        vp_banner = findViewById(R.id.vp_banner);
        rcv_classify = findViewById(R.id.rcv_classify);
        rcv_list = findViewById(R.id.rcv_list);

    }
}